package com.example.lecturereservation.controller;


import com.example.lecturereservation.entity.Users;
import com.example.lecturereservation.service.UsersService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.logging.Logger;

@Controller
public class UsersController {

    @Autowired
    UsersService usersService;

    //日志
    java.util.logging.Logger logger = Logger.getLogger(this.getClass().getName());

    //注册功能
    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody Map<String, String> maps, HttpServletRequest request) {

        logger.info("用户开始登录了...");

        String email = maps.get("email");
        String password = maps.get("password");

        logger.info("登录邮箱：" + email);
        logger.info("登录密码：" + password);

        if (email.length() == 0) return "请输入邮箱！";
        Users users = this.usersService.findByEmail(email);
        if (users == null) return "邮箱不存在";
        if (!users.getPassword().equals(password)) return "密码错误";

        //网站使用会话技术（每个用户拥有自己的会话）记住用户.30分钟
        request.getSession().setAttribute("users", users);

        logger.info("用户登录成功！");

        return "ok";
    }

    //获取已登录用户
    @RequestMapping("/getLoginUsers")
    @ResponseBody
    public Users getLoginUsers(HttpServletRequest request) {
        //通过session获取登录的用户
        Users users = (Users) request.getSession().getAttribute("users");
        users.setPassword("********");
        //判断用户是否登录
        if (users == null) {
            logger.info("用户未登录，非法请求！");
            users = new Users();
            users.setId(-1);
            return users;
        }
        return users;
    }

    //退出登录
    @RequestMapping("/unlogin")
    @ResponseBody
    public String unlogin(HttpServletRequest request) {
        request.getSession().setAttribute("users", null);
        return "ok";
    }

    @RequestMapping("/registration")
    @ResponseBody
    public String registration(@RequestBody Map<String, String> maps) {

        logger.info("开始注册...");

        //获取参数
        String email = maps.get("email");
        String password = maps.get("password");
        String nickname = maps.get("nickname");
        Integer age = Integer.valueOf(maps.get("age"));
        Integer gender = Integer.valueOf(maps.get("gender"));
        Integer telephone = Integer.valueOf(maps.get("telephone"));
        String address = maps.get("address");

        //创建用户
        Users users = new Users();
        users.setEmail(email);
        users.setPassword(password);
        users.setNickname(nickname);
        users.setAge(age);
        users.setGender(gender);
        users.setTelephone(telephone);
        users.setAddress(address);

        //注册用户
        return this.usersService.registration(users);

    }
}